package com.example.app.view.c1;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.videoplay.R;
import com.example.app.activity.MLog;
import com.example.app.manager.c1.Camera1Manager;
import com.example.app.view.PreviewBaseLayout;

/**
 * Created by 郭敏 on 2018/3/21 0021.
 */
public class PreviewC1Layout extends PreviewBaseLayout {


    public PreviewC1Layout(Context context) {
        super(context);
    }

    public PreviewC1Layout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PreviewC1Layout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private SurfaceView surface;
    private PreviewC1View previewView;
    private View focusView;
    private int focusW = 250, focusH = 250;

    @Override
    protected void init(Context context) {

    }

    public void onInitView(Context context, boolean isSurfaceView) {
        // SurfaceView 和 TextureView 二选一
        if (isSurfaceView) {
            surface = getSurfaceView(context);
            addView(surface);
        } else {
            previewView = getTextureView(context);
            addView(previewView);
        }
        focusView = getFocusView(context);
        addView(focusView);
        focusView.setVisibility(View.INVISIBLE);
    }

    protected SurfaceView getSurfaceView(Context context) {
        SurfaceView surfaceView = new SurfaceView(context);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        surfaceView.setLayoutParams(lp);
        return surfaceView;
    }

    protected PreviewC1View getTextureView(Context context) {
        PreviewC1View previewC1View = new PreviewC1View(context);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        previewC1View.setLayoutParams(lp);
        return previewC1View;
    }

    protected View getFocusView(Context context) {
        TextView tv = new TextView(context);
        tv.setBackgroundResource(R.drawable.click_focus_bg);
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        tv.setLayoutParams(lp);
        return tv;
    }

    private boolean isCalculate;

    //设置 宽和高
    private void setFocusViewWH() {
        if (focusView == null) {
            return;
        }
        if (isCalculate) {
            return;
        }
        isCalculate = true;
        ViewGroup.LayoutParams lp = focusView.getLayoutParams();
        lp.width = focusW;
        lp.height = focusH;
        focusView.setLayoutParams(lp);
    }

    public SurfaceView getSurfaceView() {
        return surface;
    }

    public PreviewC1View getPreviewView() {
        return previewView;
    }

    //一般是获取播放视频是的 Surface
    public Object getSurfacePlay() {
        if (surface != null) {
            return surface;
        }
        if (previewView == null) {
            return null;
        }
        SurfaceTexture surfaceTexture = previewView
                .getSurfaceTexture();
        Surface surface = new Surface(surfaceTexture);
        return surface;
    }

    private Camera1Manager manager;

    public void setCameraManager(Camera1Manager manager) {
        this.manager = manager;
        if (previewView != null) {
            previewView.setCameraManager(manager);
        }
    }

    public boolean isAvailable() {
        if (previewView == null) {
            return true;
        }
        return previewView.isAvailable();
    }

    public void setSurfaceCallback() {
        if (previewView == null) {
            return;
        }
        previewView.setSurfaceCallback();
    }

    //0 没有 1：缩放事件 2：聚焦事件
    private int type;

    @Override
    protected void onTouch(MotionEvent event) {
        if (manager == null) {
            return;
        }
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                type = 0;
                setFocusViewWH();
                onAnimationStop();
                focusView.setVisibility(View.INVISIBLE);
            default:
                // 触摸设备时手指的数量
                int count = event.getPointerCount();
                if (count > 1) {
                    type = 2;
                    manager.setCameraZoom(event);
                    return;
                }
                if (action == MotionEvent.ACTION_UP && type == 0) {
                    //聚焦事件
                    type = 1;
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    MLog.e("--------聚焦事件", " x:" + x + " y:" + y);
                    LayoutParams lp = (LayoutParams) focusView.getLayoutParams();
                    lp.leftMargin = x - focusW / 4;
                    lp.topMargin = y - focusH / 4;
                    focusView.setLayoutParams(lp);
                    onFocusAnimator(focusView);
                }
                break;
        }
    }

    @Override
    protected void onAnimatorStart() {
        MLog.e("--------聚焦事件", " 动画开始");
        focusView.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onAnimatorEnd() {
        int left = focusView.getLeft();
        int top = focusView.getTop();
        int right = focusView.getRight();
        int bottom = focusView.getBottom();
        //
        int w = focusW / 4;
        int h = focusH / 4;
        Rect rect = new Rect(left + w, top + h,
                right - w, bottom - h);
        int rectW = rect.width();
        int rectH = rect.height();
        if (rectW < 0) {
            rect.right -= rectW;
        }
        if (rectH < 0) {
            rect.bottom -= rectH;
        }
        int x = rect.centerX();
        int y = rect.centerY();
        MLog.e("--------", " x:" + x + " y:" + y);
        focusView.setVisibility(View.INVISIBLE);
        manager.setCameraFocus(x, y);
    }
}
